/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package imageprocessor;

import converter.text2PEF.Text2PEFConverter;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import marvin.image.MarvinImage;
import marvin.image.MarvinImageMask;
import marvin.io.MarvinImageIO;
import marvin.plugin.MarvinImagePlugin;
import marvin.util.MarvinPluginLoader;

/**
 *
 * @author Diluka
 */
public class Main {

    public static final String JAVABRIDGE_PORT = "8080";
    static final php.java.bridge.JavaBridgeRunner runner =
            php.java.bridge.JavaBridgeRunner.getInstance(JAVABRIDGE_PORT);

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        try {
            runner.waitFor();
            System.exit(0);
        } catch (InterruptedException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE,
                    null, ex);
        }
    }

    public String processImageByUrl(String url,String name) {
        name= name.replaceAll("[/:?'<>|\\\"]+","");
         BufferedImage image = null;
        try {
            image = ImageIO.read(new URL(url));
        } catch (Exception ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }
        String path=this.processImage(image,name);
                return path;
    }
    public String processImageByLocation(String location,String name){
          BufferedImage image = null;
        try {
            image = ImageIO.read(new File(location));
        } catch (Exception ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }
       String path=this.processImage(image,name);
                return path;
    }

    public String processImage(BufferedImage image,String name) {
       
        MarvinImage marvinImage = new MarvinImage(image);
        marvinImage.resize(78, 78);
        MarvinImagePlugin imagePlugin =
                MarvinPluginLoader.loadImagePlugin(
                "org.marvinproject.image.color.grayScale.jar");
        imagePlugin.process(marvinImage, marvinImage, null, MarvinImageMask.NULL_MASK, true);
        imagePlugin = MarvinPluginLoader.loadImagePlugin("org.marvinproject.image.edge.edgeDetector.jar");
        imagePlugin.process(marvinImage, marvinImage, null, MarvinImageMask.NULL_MASK, false);
        marvinImage.update();


        MarvinImageIO.saveImage(marvinImage, name);  //save to a path where gray images stored like  MarvinImageIO.saveImage(marvinImage, "H:\\indiaCpy.PNG");

        // read "any" type of image (in this case a png file)
        try {
            image = ImageIO.read(new File(name));
        } catch (IOException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }

        // write it to byte array in-memory (jpg format)
        ByteArrayOutputStream b = new ByteArrayOutputStream();
        try {
            ImageIO.write(image, "png", b);
        } catch (IOException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }

        // do whatever with the array...
        byte[] byteArray = b.toByteArray();

        // convert it to a String with 0s and 1s        
        StringBuilder sb = new StringBuilder();
        for (byte by : byteArray) {
            sb.append(Integer.toBinaryString(by & 0xFF));
        }

        getRGB(image,name);
         Text2PEFConverter txt2pef=new Text2PEFConverter();
         String path=txt2pef.text2PEF(name);

        return path;
    }

    public static void getRGB(BufferedImage image,String name) {

        try {
            int[][] result = new int[image.getHeight()][image.getWidth()];
            int[][] r = new int[image.getHeight()][image.getWidth()];
            int[][] g = new int[image.getHeight()][image.getWidth()];
            int[][] b = new int[image.getHeight()][image.getWidth()];

            int tempR = 0;
            int tempG = 0;
            int tempB = 0;

            int[] rgb;
            for (int i = 0; i < image.getHeight(); i++) {
                for (int j = 0; j < image.getWidth(); j++) {
                    rgb = getPixelData(image, i, j);
                    tempR = rgb[0];
                    tempG = rgb[1];
                    tempB = rgb[2];

                    if (tempR > 170 && tempG > 170 && tempB > 170) {
                        result[i][j] = 0;
                    } else {
                        result[i][j] = 1;
                    }
                }
            }

            FileWriter file = new FileWriter("C:\\Users\\CSE\\Documents\\NetBeansProjects\\ImageProcessor\\src\\converter\\textloc\\"+name+".txt");
            BufferedWriter writer = new BufferedWriter(file);
            for (int i = 0; i < image.getHeight(); i += 3) {
                for (int j = 0; j < image.getWidth(); j += 2) {
                    if (result[i][j] == 0) {
                        if (result[i][j + 1] == 0) {
                            if (result[i + 1][j] == 0) {
                                if (result[i + 1][j + 1] == 0) {
                                    if (result[i + 2][j] == 0) {
                                        if (result[i + 2][j + 1] == 0) {
                                            writer.write(' ');
                                        } else {
                                            writer.write(',');
                                        }
                                    } else {
                                        if (result[i + 2][j + 1] == 0) {
                                            writer.write('\"');
                                        } else {
                                            writer.write(';');
                                        }
                                    }
                                } else {
                                    if (result[i + 2][j] == 0) {
                                        if (result[i + 2][j + 1] == 0) {
                                            writer.write('@');
                                        } else {
                                            writer.write('.');
                                        }
                                    } else {
                                        if (result[i + 2][j + 1] == 0) {
                                            writer.write('^');
                                        } else {
                                            writer.write('_');
                                        }
                                    }
                                }
                            } else {
                                if (result[i + 1][j + 1] == 0) {
                                    if (result[i + 2][j] == 0) {
                                        if (result[i + 2][j + 1] == 0) {
                                            writer.write("'");
                                        } else {
                                            writer.write('-');
                                        }
                                    } else {
                                        if (result[i + 2][j + 1] == 0) {
                                            writer.write('9');
                                        } else {
                                            writer.write('0');
                                        }
                                    }
                                } else {
                                    if (result[i + 2][j] == 0) {
                                        if (result[i + 2][j + 1] == 0) {
                                            writer.write('/');
                                        } else {
                                            writer.write('+');
                                        }
                                    } else {
                                        if (result[i + 2][j + 1] == 0) {
                                            writer.write('>');
                                        } else {
                                            writer.write('#');
                                        }
                                    }
                                }
                            }
                        } else {
                            if (result[i + 1][j] == 0) {
                                if (result[i + 1][j + 1] == 0) {
                                    if (result[i + 2][j] == 0) {
                                        if (result[i + 2][j + 1] == 0) {
                                            writer.write('1');
                                        } else {
                                            writer.write('5');
                                        }
                                    } else {
                                        if (result[i + 2][j + 1] == 0) {
                                            writer.write('3');
                                        } else {
                                            writer.write('4');
                                        }
                                    }
                                } else {
                                    if (result[i + 2][j] == 0) {
                                        if (result[i + 2][j + 1] == 0) {
                                            writer.write('I');
                                        } else {
                                            writer.write('[');
                                        }
                                    } else {
                                        if (result[i + 2][j + 1] == 0) {
                                            writer.write('J');
                                        } else {
                                            writer.write('W');
                                        }
                                    }
                                }
                            } else {
                                if (result[i + 1][j + 1] == 0) {
                                    if (result[i + 2][j] == 0) {
                                        if (result[i + 2][j + 1] == 0) {
                                            writer.write('2');
                                        } else {
                                            writer.write('8');
                                        }
                                    } else {
                                        if (result[i + 2][j + 1] == 0) {
                                            writer.write('6');
                                        } else {
                                            writer.write('7');
                                        }
                                    }
                                } else {
                                    if (result[i + 2][j] == 0) {
                                        if (result[i + 2][j + 1] == 0) {
                                            writer.write('S');
                                        } else {
                                            writer.write('!');
                                        }
                                    } else {
                                        if (result[i + 2][j + 1] == 0) {
                                            writer.write('T');
                                        } else {
                                            writer.write(')');
                                        }
                                    }
                                }
                            }
                        }
                    } else {
                        if (result[i][j + 1] == 0) {
                            if (result[i + 1][j] == 0) {
                                if (result[i + 1][j + 1] == 0) {
                                    if (result[i + 2][j] == 0) {
                                        if (result[i + 2][j + 1] == 0) {
                                            writer.write('A');
                                        } else {
                                            writer.write('*');
                                        }
                                    } else {
                                        if (result[i + 2][j + 1] == 0) {
                                            writer.write('E');
                                        } else {
                                            writer.write(':');
                                        }
                                    }
                                } else {
                                    if (result[i + 2][j] == 0) {
                                        if (result[i + 2][j + 1] == 0) {
                                            writer.write('C');
                                        } else {
                                            writer.write('%');
                                        }
                                    } else {
                                        if (result[i + 2][j + 1] == 0) {
                                            writer.write('D');
                                        } else {
                                            writer.write('?');
                                        }
                                    }
                                }
                            } else {
                                if (result[i + 1][j + 1] == 0) {
                                    if (result[i + 2][j] == 0) {
                                        if (result[i + 2][j + 1] == 0) {
                                            writer.write('K');
                                        } else {
                                            writer.write('U');
                                        }
                                    } else {
                                        if (result[i + 2][j + 1] == 0) {
                                            writer.write('O');
                                        } else {
                                            writer.write('Z');
                                        }
                                    }
                                } else {
                                    if (result[i + 2][j] == 0) {
                                        if (result[i + 2][j + 1] == 0) {
                                            writer.write('M');
                                        } else {
                                            writer.write('X');
                                        }
                                    } else {
                                        if (result[i + 2][j + 1] == 0) {
                                            writer.write('N');
                                        } else {
                                            writer.write('Y');
                                        }
                                    }
                                }
                            }
                        } else {
                            if (result[i + 1][j] == 0) {
                                if (result[i + 1][j + 1] == 0) {
                                    if (result[i + 2][j] == 0) {
                                        if (result[i + 2][j + 1] == 0) {
                                            writer.write('B');
                                        } else {
                                            writer.write('<');
                                        }
                                    } else {
                                        if (result[i + 2][j + 1] == 0) {
                                            writer.write('H');
                                        } else {
                                            writer.write('\\');
                                        }
                                    }
                                } else {
                                    if (result[i + 2][j] == 0) {
                                        if (result[i + 2][j + 1] == 0) {
                                            writer.write('F');
                                        } else {
                                            writer.write('$');
                                        }
                                    } else {
                                        if (result[i + 2][j + 1] == 0) {
                                            writer.write('G');
                                        } else {
                                            writer.write(']');
                                        }
                                    }
                                }
                            } else {
                                if (result[i + 1][j + 1] == 0) {
                                    if (result[i + 2][j] == 0) {
                                        if (result[i + 2][j + 1] == 0) {
                                            writer.write('L');
                                        } else {
                                            writer.write('V');
                                        }
                                    } else {
                                        if (result[i + 2][j + 1] == 0) {
                                            writer.write('R');
                                        } else {
                                            writer.write('(');
                                        }
                                    }
                                } else {
                                    if (result[i + 2][j] == 0) {
                                        if (result[i + 2][j + 1] == 0) {
                                            writer.write('P');
                                        } else {
                                            writer.write('&');
                                        }
                                    } else {
                                        if (result[i + 2][j + 1] == 0) {
                                            writer.write('Q');
                                        } else {
                                            writer.write('=');
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                writer.newLine();
            }
            writer.flush();
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static int[] getPixelData(BufferedImage image, int x, int y) {
        int argb = image.getRGB(y, x);

        int rgb[] = new int[]{
            (argb >> 16) & 0xff, //red
            (argb >> 8) & 0xff, //green
            (argb) & 0xff //blue
        };

        return rgb;
    }
}
